// src/utils/ToolPathGenerator.ts
import * as THREE from 'three';

// 生成刀具路径点
export function generateToolPath(): number[][] {
  const path: number[][] = [];
  
  // 1. 起始位置（工件上方）
  path.push([0, 10, 0]);
  
  // 2. 缓慢下降接近工件
  for (let y = 10; y > 6; y -= 0.5) {
    path.push([0, y, 0]);
  }
  
  // 3. 直线切削
  for (let z = 0; z < 4; z += 0.4) {
    path.push([0, 6, z]);
  }
  
  // 4. 提升刀具
  path.push([0, 7, 4]);
  
  // 5. 移动到下一个位置
  for (let x = 0; x <= 2; x += 0.4) {
    path.push([x, 7, 4]);
  }
  
  // 6. 再次下降
  for (let y = 7; y > 4; y -= 0.5) {
    path.push([2, y, 4]);
  }
  
  // 7. 曲线运动
  const points = createCurvePoints([2, 4, 4], [-3, 2, -2]);
  path.push(...points);
  
  // 8. 最终位置
  path.push([-3, 8, -2]);
  
  return path;
}

// 生成曲线路径点
function createCurvePoints(start: number[], end: number[]): number[][] {
  const curvePoints: number[][] = [];
  const numPoints = 20;
  
  // 创建三维曲线
  const curve = new THREE.CubicBezierCurve3(
    new THREE.Vector3(start[0], start[1], start[2]),
    new THREE.Vector3(0, 6, 0),
    new THREE.Vector3(-1.5, 5, -3),
    new THREE.Vector3(end[0], end[1], end[2])
  );
  
  // 采样曲线点
  for (let i = 0; i <= numPoints; i++) {
    const point = curve.getPoint(i / numPoints);
    curvePoints.push([point.x, point.y, point.z]);
  }
  
  return curvePoints;
}